package lexicalUnit;

import chalmersbot.Absyn.QHowManySubVerbSub;
import chalmersbot.Absyn.QHowManySubVerbSubVerb;
import chalmersbot.Absyn.QInWhichSubIsSub;
import chalmersbot.Absyn.QInWhichSubIsSubVerb;
import chalmersbot.Absyn.QInWhichSubIsSubVerbSub;
import chalmersbot.Absyn.QInWhichSubVerbSub;
import chalmersbot.Absyn.QInWhichSubVerbSubVerb;
import chalmersbot.Absyn.QInWhichSubVerbSubVerbSub;
import chalmersbot.Absyn.QWordIsSub;
import chalmersbot.Absyn.QWordIsSubVerb;
import chalmersbot.Absyn.QWordIsSubVerbSub;
import chalmersbot.Absyn.QWordSubIsSub;
import chalmersbot.Absyn.QWordSubIsSubVerb;
import chalmersbot.Absyn.QWordSubVerb;
import chalmersbot.Absyn.QWordSubVerbSub;
import chalmersbot.Absyn.QWordVerbSub;
import chalmersbot.Absyn.QWordVerbSubVerb;
import chalmersbot.Absyn.QWordVerbSubVerbSub;
import chalmersbot.Absyn.QYNIsSub;
import chalmersbot.Absyn.QYNIsSubVerbSub;
import chalmersbot.Absyn.QYNVerbSubVerb;
import chalmersbot.Absyn.QYNVerbSubVerbSub;
import chalmersbot.Absyn.Phrase.Visitor;

public class CheckPhrase implements Visitor<String, Environ> {

	@Override
	public String visit(QWordIsSub p, Environ env) {
		env.setVerbKeyword(Environ.VERB_IS);
		env.setSubject(p.subject_.accept(new CheckSubject(), env));
		env.setVerb(p.isvrb_.accept(new CheckIsVrb(),env));
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QWordIsSubVerb p, Environ env) {
		env.setSubject(p.subject_.accept(new CheckSubject(), env));
		env.setVerb(p.isvrb_.accept(new CheckIsVrb(),env) + " " + p.verb_.accept(new CheckVerb(), env));
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QYNVerbSubVerbSub p, Environ env) {
		throw new LexicalException("Yes/No questions are not supported yet");
	}

	@Override
	public String visit(QYNIsSubVerbSub p, Environ env) {
		throw new LexicalException("Yes/No questions are not supported yet");
	}

	@Override
	public String visit(QWordIsSubVerbSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String isVerb = p.isvrb_.accept(new CheckIsVrb(), env);
		env.setFlag(Environ.ISVERB_TENSE);
		env.setVerb(isVerb + " " + p.verb_.accept(new CheckVerb(), env));
		env.setRes(p.subject_2.accept(new CheckSubject(), env));
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QWordSubVerb p, Environ env) {
		throw new LexicalException("Wrong question syntax");
	}

	@Override
	public String visit(QWordSubVerbSub p, Environ env) {
		env.setKind_of_answer(Environ.FOLLOWING_ANSWER);
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		env.setVerb(p.verb_.accept(new CheckVerb(), env));
		env.setRes(p.subject_2.accept(new CheckSubject(), env));
		p.qword_.accept(new CheckQWord(), env);
		//return "the following " + env.getSubject() + " " + env.getVerb() + env.getRes() + ": " + env.generateAnswer();
		return null;
	}

	@Override
	public String visit(QWordSubIsSub p, Environ env) {
		env.setSubject("the " + p.subject_1.accept(new CheckSubject(), env));
		env.setVerb(p.isvrb_.accept(new CheckIsVrb(), env));
		p.subject_2.accept(new CheckSubject(), env);
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QWordVerbSub p, Environ env) {
		env.setSubject(p.subject_.accept(new CheckSubject(), env));
		String verb = p.verb_.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)
			throw new LexicalException("The auxiliar verb " + verb + " should be with a main verb");
		env.setVerb(verb);
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QWordVerbSubVerb p, Environ env) {
		env.setSubject(p.subject_.accept(new CheckSubject(), env));
		String verb = p.verb_1.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)
			env.setVerb(env.toVerbTense(p.verb_2.accept(new CheckVerb(), env)));
		else
			env.setVerb(verb + " " + p.verb_2.accept(new CheckVerb(), env));
		return p.qword_.accept(new CheckQWord(), env);
	}

	@Override
	public String visit(QWordVerbSubVerbSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String auxVerb = p.verb_1.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)		
			env.setVerb(env.toVerbTense(p.verb_2.accept(new CheckVerb(), env)));
		else
			env.setVerb(auxVerb + " " + p.verb_2.accept(new CheckVerb(), env));
		env.setRes(p.subject_2.accept(new CheckSubject(), env));
		return p.qword_.accept(new CheckQWord(), env);
	}

	

	@Override
	public String visit(QHowManySubVerbSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String verb = p.verb_.accept(new CheckVerb(), env);
		if (!verb.startsWith("are"))
			throw new LexicalException("Wrong verb tense");
		p.subject_2.accept(new CheckSubject(), env);
		return p.qwhow_.accept(new CheckQWHow(), env);
	}

	@Override
	public String visit(QHowManySubVerbSubVerb p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String auxVerb = p.verb_1.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)		
			env.setVerb(p.verb_2.accept(new CheckVerb(), env));
		else
			env.setVerb(auxVerb + " " + p.verb_2.accept(new CheckVerb(), env));
		p.subject_2.accept(new CheckSubject(), env);
		return p.qwhow_.accept(new CheckQWHow(), env);
	}

	@Override
	public String visit(QYNVerbSubVerb p, Environ env) {
		throw new LexicalException("Yes/No questions are not supported yet");
	}

	@Override
	public String visit(QYNIsSub p, Environ env) {
		throw new LexicalException("Yes/No questions are not supported yet");
	}
	
	@Override
	public String visit(QInWhichSubIsSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		env.setVerb(p.isvrb_.accept(new CheckIsVrb(), env));
		p.subject_2.accept(new CheckSubject(), env);
		return p.qwinw_.accept(new CheckQWInW(), env);
	}

	@Override
	public String visit(QInWhichSubIsSubVerbSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String isVerb = p.isvrb_.accept(new CheckIsVrb(), env);
		env.setFlag(Environ.ISVERB_TENSE);
		env.setVerb(isVerb + " " + p.verb_.accept(new CheckVerb(), env));
		env.setRes(p.subject_2.accept(new CheckSubject(), env) + " " + p.subject_3.accept(new CheckSubject(), env));		
		return p.qwinw_.accept(new CheckQWInW(), env);
	}

	@Override
	public String visit(QInWhichSubVerbSub p, Environ env) {
		throw new LexicalException("Wrong question syntax");
	}

	@Override
	public String visit(QInWhichSubVerbSubVerbSub p, Environ env) {
		env.setSubject(p.subject_1.accept(new CheckSubject(), env));
		String auxVerb = p.verb_1.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)		
			env.setVerb(p.verb_2.accept(new CheckVerb(), env));
		else
			env.setVerb(auxVerb + " " + p.verb_2.accept(new CheckVerb(), env));
		env.setRes(p.subject_2.accept(new CheckSubject(), env) + " " + p.subject_3.accept(new CheckSubject(), env));
		return p.qwinw_.accept(new CheckQWInW(), env);
	}

	@Override
	public String visit(QInWhichSubIsSubVerb p, Environ env) {
		p.subject_1.accept(new CheckSubject(), env);
		env.setSubject(p.subject_2.accept(new CheckSubject(), env));
		String isVerb = p.isvrb_.accept(new CheckIsVrb(), env);
		env.setFlag(Environ.ISVERB_TENSE);
		env.setVerb(isVerb + " " + p.verb_.accept(new CheckVerb(), env));	
		return p.qwinw_.accept(new CheckQWInW(), env);
	}

	@Override
	public String visit(QInWhichSubVerbSubVerb p, Environ env) {
		p.subject_1.accept(new CheckSubject(), env);
		env.setSubject(p.subject_2.accept(new CheckSubject(), env));
		String auxVerb = p.verb_1.accept(new CheckVerb(), env);
		if (env.resetFlag()==Environ.AUX_VERB)		
			env.setVerb(p.verb_2.accept(new CheckVerb(), env));
		else
			env.setVerb(auxVerb + " " + p.verb_2.accept(new CheckVerb(), env));	
		return p.qwinw_.accept(new CheckQWInW(), env);
	}

	@Override
	public String visit(QWordSubIsSubVerb p, Environ arg) {
		// TODO Auto-generated method stub
		return null;
	}

}
